﻿using System;

public class Node<T>
{
    public Node<T> Next { get; set; }
    public T Data { get; set; }

    public Node(T t)
    {
        Next = null;
        Data = t;
    }
}

public class Genericlist<T>
{
    private Node<T> head;
    private Node<T> tail;

    public Genericlist()
    {
        tail = head = null;
    }

    public Node<T> Head => head;

    public void Add(T t)
    {
        Node<T> newNode = new Node<T>(t);
        if (head == null)
        {
            head = tail = newNode;
        }
        else
        {
            tail.Next = newNode;
            tail = newNode;
        }
    }

    public void ForEach(Action<T> action)
    {
        for (Node<T> node = head; node != null; node = node.Next)
        {
            action(node.Data);
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        // 整型List
        Genericlist<int> intList = new Genericlist<int>();
        for (int x = 0; x < 10; x++)
        {
            intList.Add(x);
        }

        Console.WriteLine("Printing integers:");
        intList.ForEach(Console.WriteLine);

        // 字符串型List
        Genericlist<string> strList = new Genericlist<string>();
        for (int x = 0; x < 10; x++)
        {
            strList.Add("str" + x);
        }

        Console.WriteLine("\nPrinting strings:");
        strList.ForEach(Console.WriteLine);

        // 求和
        int sum = 0;
        intList.ForEach(x => sum += x);
        Console.WriteLine("\nSum: " + sum);

        // 最大值
        int max = int.MinValue;
        intList.ForEach(x => { if (x > max) max = x; });
        Console.WriteLine("Max: " + max);

        // 最小值
        int min = int.MaxValue;
        intList.ForEach(x => { if (x < min) min = x; });
        Console.WriteLine("Min: " + min);
    }
}
